package com.kecheng.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.kecheng.dao.TongjiDao;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import com.kecheng.pojo.Tongji;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

@Action(value = "tongji", results = {
		@Result(name = "success", location = "/admin/tongji/tongji.jsp"),
		@Result(name = "success2", location = "/admin/tongji/tongji1.jsp"),
		@Result(name = "success3", location = "/admin/tongji/tongji2.jsp"),
})
@AllowedMethods(value = {("regex:.*")})
public class TongjiAction {
	private Tongji tongji;
	@Autowired
	private TongjiDao tongjiDao;
	private List<Tongji> tongjis;
	private String time1;
	private String time2;
	private Tongji tongji1;
	
	public String export()throws Exception{  
	      //获取需要导出的对象的list
	    	tongjis=tongjiDao.findbytime2(time1, time2); 
	        String[] tableHeader={"来源日期","付款订单","成交额","客单价","新增分销商","订单总数","分销商总数"};
	        
	        short cellNumber=(short)tableHeader.length;//表的列数
	        HSSFWorkbook workbook = new HSSFWorkbook();   //创建一个excel
	        HSSFCell cell = null;                                    //Excel的列
	        HSSFRow row = null;                                      //Excel的行
	        HSSFCellStyle style = workbook.createCellStyle();        //设置表头的类型
	        style.setAlignment(CellStyle.ALIGN_CENTER);
	        HSSFCellStyle style1 = workbook.createCellStyle();       //设置数据类型
	        style1.setAlignment(CellStyle.ALIGN_CENTER);
	        HSSFFont font = workbook.createFont();                 //设置字体
	        HSSFSheet sheet = workbook.createSheet("sheet1");        //创建一个sheet
	        HSSFHeader header = sheet.getHeader();//设置sheet的头
	            try {              
	            if(tongjis.size() < 1 ){
	                header.setCenter("无统计信息");
	            }else{
	                header.setCenter("统计信息列表");
	                row = sheet.createRow(0);
	                row.setHeight((short)400);
	                for(int k = 0;k < cellNumber;k++){
	                	
	                    cell = row.createCell((short)k);//创建第0行第k列
	                  //  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	                    cell.setCellValue(tableHeader[k]);//设置第0行第k列的值
	                    short y=(short)6000;
	                    sheet.setColumnWidth((short)k,y);//设置列的宽度
	                    font.setColor(Font.COLOR_NORMAL);      // 设置单元格字体的颜色.
	                    font.setFontHeight((short)250); //设置单元字体高度
	                    style1.setFont(font);//设置字体风格
	                    cell.setCellStyle(style1);
	                }
	                  //开始循环写入    
	                for(int i = 0 ;i < tongjis.size();i++){   
	                	
	                	tongji = tongjis.get(i);//获取student对象
	                    row = sheet.createRow((short) (i + 1));//创建第i+1行
	                    row.setHeight((short)400);//设置行高
	                   
	             
	                    if(tongji.getTime() != null){
	                         cell = row.createCell((short)0); //创建第i+1行第0列
	                      //   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	                         cell.setCellValue(tongji.getTime());//设置第i+1行第1列的值
	 
	                         cell.setCellStyle(style); //设置风格
	                    }
	         //由于下面的和上面的基本相同，就不加注释了
	                    
	                    if(tongji.getPay()!= null){
	                         cell = row.createCell((short)1);
	                        // cell.setEncoding();
	                         cell.setCellValue(tongji.getPay());
	                         cell.setCellStyle(style);
	                    }
	                  
	                    
	                    if(tongji.getMoney() != null){
	                        cell = row.createCell((short)2);
	                     //   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	                        cell.setCellValue(tongji.getMoney());
	                        cell.setCellStyle(style);
	                   }
	                    
	                    if(tongji.getPrice() != null){
	                        cell = row.createCell((short)3);
	                //      cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	                        cell.setCellValue(tongji.getPrice());
	                        cell.setCellStyle(style);
	                   }
	                    
	                    if(tongji.getAllorder() != null){
	                        cell = row.createCell((short)5);
	                    //    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	                        cell.setCellValue(tongji.getAllorder());
	                        cell.setCellStyle(style);
	              
	                   }
	                    
	                    if(tongji.getAllteam() != null){
	                        cell = row.createCell((short)6);
	                     //   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	                        cell.setCellValue(tongji.getAllteam());
	                        cell.setCellStyle(style);
	              
	                   }
	                    
	                    
	                    if(tongji.getTeam()!= null){
	                        cell = row.createCell((short)4);
	                      //  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	                        cell.setCellValue(tongji.getTeam());
	                        cell.setCellStyle(style);
	              
	                   }
	                   
	                }//
	               
	            }
	           
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	       
	  
	        HttpServletResponse response = null;//创建一个HttpServletResponse对象
	        OutputStream out = null;//创建一个输出流对象
	        try {
	            response = ServletActionContext.getResponse();//初始化HttpServletResponse对象
	            out = response.getOutputStream();//
	          
	    String headerStr = "统计信息列表";
	    headerStr = new String(headerStr.getBytes("utf-8"), "ISO8859-1");//headerString为中文时转码
	    response.setHeader("Content-disposition","attachment; filename="+    headerStr+".xls");//filename是下载的xls的名，建议最好用英文
	            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
	            response.setHeader("Pragma","No-cache");//设置头
	            response.setHeader("Cache-Control","no-cache");//设置头
	            response.setDateHeader("Expires", 0);//设置日期头
	            workbook.write(out);
	            out.flush();
	            workbook.write(out);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally{
	            try{
	               
	                if(out!=null){
	                    out.close();
	                }
	               
	            }catch(IOException e){
	                e.printStackTrace();
	            }
	           
	        }
	    	
	        return null;
	   }
	    
	
	
	public String findbytime1(){
		
		tongjis=tongjiDao.findbytime1();
		
		return "success";
	}
	
	public String findbytime2(){
		
		
		Integer pay=0;		
		Double money=0.0;
		Double price=0.0;
	    Integer team=0;
		Integer allorder=0;
		Integer allteam=0;
		
		
		tongjis=tongjiDao.findbytime2(time1,time2);
		
		tongji1=new Tongji();
		for(int i=0;i<tongjis.size();i++){
			
			pay=pay+tongjis.get(i).getPay();
			tongji1.setPay(pay);
					
			money=money+tongjis.get(i).getMoney();
			tongji1.setMoney(money);
			
			team=team+tongjis.get(i).getTeam();
			tongji1.setTeam(team);
			
			tongji1.setAllorder(tongjis.get(i).getAllorder());

			tongji1.setAllteam(tongjis.get(i).getAllteam());
		
		}
		if(tongji1.getMoney()==0||tongji1.getPay()==0){
			tongji1.setPrice(0.0);
		}else{
			
			  java.text.DecimalFormat   df=new   java.text.DecimalFormat("#.##"); 
			  double price1=tongji1.getMoney()/tongji1.getPay();
			  price1=((int)(price1*100))/100;   
			  tongji1.setPrice(price1); 

			
		}

		tongjis=tongjiDao.findbytime1();
		return "success2";
	}
	
	public String findbytime3(){
		
		
		tongjis=tongjiDao.findbytime3(time1,time2);
		
		return "success3";
	}
	
	

	public Tongji getTongji() {
		return tongji;
	}

	public void setTongji(Tongji tongji) {
		this.tongji = tongji;
	}

	public List<Tongji> getTongjis() {
		return tongjis;
	}

	public void setTongjis(List<Tongji> tongjis) {
		this.tongjis = tongjis;
	}

	public String getTime1() {
		return time1;
	}

	public void setTime1(String time1) {
		this.time1 = time1;
	}

	public String getTime2() {
		return time2;
	}

	public void setTime2(String time2) {
		this.time2 = time2;
	}

	public Tongji getTongji1() {
		return tongji1;
	}

	public void setTongji1(Tongji tongji1) {
		this.tongji1 = tongji1;
	}

	
	
	
	
}
